package org.ttitfly.ttcore.util.helper;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class ShipperOrderHelper {
    public static String get56Num(String cur56Num) {
        if (!code56Validate(cur56Num)) {
            return null;
        }

        return getNext56Num(cur56Num, 1);
    }

    public static List<String> get56NumList(String cur56Num, int num) {
        if (!code56Validate(cur56Num)) {
            return null;
        }
        List<String> ret = new ArrayList<String>();
        String next56Num = get56Num(cur56Num);
        ret.add(next56Num);
        int loop = 1;
        while (loop < num) {
            loop = loop + 1;
            next56Num = get56Num(next56Num);
            ret.add(next56Num);
        }

        return ret;
    }

    private static boolean code56Validate(String cur56Num) {
        if (cur56Num.length() != 12) {
            return false;
        }

        return cur56Num.equals(getNext56Num(cur56Num, 0));
    }

    private static String getNext56Num(String cur56Num, int add) {
        String pre56 = StringUtils.substring(cur56Num, 0, 4);
        String basis56 = StringUtils.substring(cur56Num, 4, cur56Num.length() - 1);
        String next56 = pre56 + String.valueOf(Long.parseLong(basis56) + add);
        /**
         *verify code producing rule
         */
        int sum = 0;
        for (int i = 0; i < next56.length(); i++) {
            if (i % 2 == 0) {
                sum = sum + (Integer.parseInt(String.valueOf(next56.charAt(i))) * 3);
            } else {
                sum = sum + (Integer.parseInt(String.valueOf(next56.charAt(i))));
            }
        }
        int mod = (10 - (sum % 10)) % 10;

        return next56 + String.valueOf(mod);
    }

    public static void main(String... s) {
        List<String> ret = get56NumList("565611778170", 10);
        for (String ss : ret) {
            System.out.println(ss);
        }
    }
}
